<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/css/layui.min.2.9.21.css">
  <link rel="stylesheet" href="/css/index.css">
</head>

<body>

  <div class="layui-row main">
    <div class="layui-col-sm2 layui-col-xs0"> </div>
    <div class="layui-col-sm8 layui-col-xs12" id="content">

      <div class="flow-demo-show" id="ID-flow-demo-manual"></div>


      <div style="display: flex ; flex-direction: row; justify-content: space-between; font-size: 20px;">
        <a id="prev" href="http://" style="width: 50%; text-align: center;">上一章</a>
        <a id="mulu" href="http://" style="width: 50%; text-align: center;">目录</a>
        <a id="next" href="http://" style="width: 50%; text-align: center;">下一章</a>
      </div>


      <hr class="layui-border-green">
      <hr class="layui-border-green">
      <hr class="layui-border-green">
      <hr class="layui-border-green">
      <hr class="layui-border-green">
      <hr class="layui-border-green">
      <hr class="layui-border-green">
      <hr class="layui-border-green" id="sldjife" style="border-bottom-width: 1000px;">
      <hr class="layui-border-green">
      <hr class="layui-border-green">
      <hr class="layui-border-green">
      <hr class="layui-border-green">
      <hr class="layui-border-green">
      <hr class="layui-border-green">
      <hr class="layui-border-green">

    </div>
    <div class="layui-col-sm2 layui-col-xs0"> </div>


  </div>

  <script src="/js/jquery.min.js"></script>
  <script src="/js/crypto-js.js"></script>
  <script src="/js/webp-hero.bundle.js"></script>
  <!-- 需要 main_load_img.js 来加载图片。且img标签的src为空才行,真实的图片地址在data-r-src属性中 -->
  <script src="/js/main_load_img.js"></script>
  <script src="/js/layui.min.2.9.21.js"></script>
  <script src="../js/axios.js"></script>
  <script>

    // let search_ = urlToJson().search_
    // document.title = decodeURIComponent(search_?.name) + decodeURIComponent(search_?.chapter_title);

    layui.use(function () {
      var layer = layui.layer;
      var form = layui.form;
      var table = layui.table;
      var util = layui.util;
      var flow = layui.flow;

      // 流加载实例
      flow.load({
        elem: '#ID-flow-demo-manual', // 流加载容器
        scrollElem: '#ID-flow-demo-manual', // 滚动条所在元素，一般不用填，此处只是演示需要。
        isAuto: false,
        isLazyimg: true,
        moreText: ' ',
        done: function (page, next) { // 加载下一页
          axios.get('/getChapterImg?id=' + urlToJson().search_.id)
            .then(function (response) {
              console.log(response);
              let { data } = response;
              data.sort((a, b) => a.imgs_index - b.imgs_index);
              let html = '';

              data.forEach(element => {
                html += `
                <li class="itemBook">
                  <img src="https://mwappimgs.cc/static/images/imagecover.png" data-r-src="${element.base_url}${element.imgs_url}" class="showImg lazy_img">
                </li>`;
              });

              // let imgs = "";
              // data.forEach(element => {
              //   imgs += `
              //     <img src="https://mwappimgs.cc/static/images/imagecover.png" data-r-src="${element.base_url}${element.imgs_url}" class="showImg lazy_img">
              //      `;
              // });
              // imgs && (html += `<li class="itemBook">${imgs}</li>`);

              next(html || `<li class="itemBook">没有更多了</li>`, true)
              loadImg(); // 加载图片

              setTimeout(() => {
                // 重新加载下图片，避免图片加载完后不显示
                let id = urlToJson().search_.id.split('_')[1] || 0;
                if (!Number(urlToJson().search_.theOne || 0)) location.href = jsonToUrl({ theOne: 1, chapter_title: `第${Number(id) + 1}章` });
              }, 5000);
            })
            .catch(function (error) {
              console.log(error), alert(error);
              next('', false)
            });
        }
      });

    });

    // setInterval(() => {
    //   var windowHeight = window.innerHeight;// 获取窗口的高度
    //   var documentHeight = document.body.scrollHeight;// 获取整个页面的高度
    //   document.getElementById('sldjife').style.borderBottomWidth = (documentHeight * 0.1 > 5000 ? 5000 : documentHeight * 0.1) + 'px';
    // }, 10);

    let content = document.getElementById('content')
    let w = Number(localStorage.getItem('w')) || 0;
    content.style.padding = `0 ${w}px`;

    function getUrl(type = 1) {
      let id = urlToJson().search_.id;
      let i = id.split('_');
      if (type == 1) {
        let ii = Number(i[1]) + 1;
        return jsonToUrl({ id: i[0] + '_' + ii, theOne: 0 })
        // return 'show.html?id=' + i[0] + '_' + ii;
      }
      else if (type == 2) {
        let ii = Number(i[1]) - 1;
        return jsonToUrl({ id: i[0] + '_' + ii, theOne: 0 })
        // return 'show.html?id=' + i[0] + '_' + ii;
      }
      else if (type == 3) {
        let ii = Number(i[1]) - 1;
        return 'book.html?id=' + i[0];
      }

    }
    $('#mulu').attr('href', getUrl(3))
    $('#prev').attr('href', getUrl(2))
    $('#next').attr('href', getUrl())

    // 自定义固定条
    var util = layui.util;
    util.fixbar({
      bars: [{ // 定义可显示的 bar 列表信息 -- v2.8.0 新增
        type: 'jia',
        icon: 'layui-icon-addition'
      }, {
        type: 'jian',
        icon: 'layui-icon-subtraction'
      }, {
        type: 'next',
        icon: 'layui-icon-next',
        style: 'background-color: #FF5722;'
      }, {
        type: 'mulu',
        icon: 'layui-icon-component',
        // style: 'background-color: #FF5722;'
      }, {
        type: 'reload',
        icon: 'layui-icon-refresh',
        // style: 'background-color: #FF5722;'
      }],
      // 点击事件
      click: function (type) {
        console.log(this, type);
        switch (type) {
          case 'jia':
            w > 0 && (w -= 20);
            content.style.padding = `0 ${w}px`;
            localStorage.setItem('w', w);
            break;
          case 'jian':
            w += 20;
            content.style.padding = `0 ${w}px`;
            localStorage.setItem('w', w);
            break;
          case 'next': location.href = getUrl(); break;
          case 'mulu': location.href = getUrl(3); break;
          case 'reload': location.reload(); break;
        }
      }
    });

  </script>
</body>

</html>